package com.xinshujia.wxxcx.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.xinshujia.wxxcx.mapper.*;
import com.xinshujia.wxxcx.pojos.entity.AlarmRecord;
import com.xinshujia.wxxcx.pojos.entity.Device;
import com.xinshujia.wxxcx.pojos.entity.FunctionName;
import com.xinshujia.wxxcx.pojos.entity.Threshold;
import com.xinshujia.wxxcx.pojos.info.*;
import com.xinshujia.wxxcx.pojos.vo.XThreshold;
import com.xinshujia.wxxcx.pojos.vo.YThreshold;
import com.xinshujia.wxxcx.pojos.vo.ZThreshold;
import com.xinshujia.wxxcx.service.ThresholdService;
import com.xinshujia.wxxcx.util.json.WriteOperation;
import com.xinshujia.wxxcx.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Author Cui Han
 * @Date 2024/5/17 17:37
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class ThresholdServiceImpl implements ThresholdService {

    @Autowired
    private ThresholdMapper thresholdMapper;

    @Autowired
    private FunctionsMapper functionsMapper;

    @Autowired
    private AlarmRecordMapper alarmRecordMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private AccelerationMapper accelerationMapper;

    @Autowired
    private MachineMapper machineMapper;

    @Autowired
    private AlarmeventlogMapper alarmeventlogMapper;


    /**
     * 根据机床id查询当前机床阈值修改记录
     *
     * @param machineId
     * @return
     */
    @Override
    public List<ThresholdInfo> findThresholdInfo(Long machineId) {
        return thresholdMapper.findThresholdInfo(machineId);
    }

    /**
     * 查询当前阈值
     *
     * @param guid
     * @return
     */
    @Override
    public List<FunctionResultInFo> findThreshold(String guid) {
        Date date = new Date();
        date.setTime(date.getTime() / 1000 * 1000);

        FunctionName functionNameX = new FunctionName();
        functionNameX.setTime(date);
        functionNameX.setDeviceGUID(guid);
        functionNameX.setFunction("{\"function\" :\"read_register_float32\",\"regaddr\":22093,\"regcount\":2}");
        functionsMapper.insterXThreshold(functionNameX);

        FunctionName functionNameY = new FunctionName();
        functionNameY.setTime(date);
        functionNameY.setDeviceGUID(guid);
        functionNameY.setFunction("{\"function\" :\"read_register_float32\",\"regaddr\":22098,\"regcount\":2}");
        functionsMapper.insterYThreshold(functionNameY);

        FunctionName functionNameZ = new FunctionName();
        functionNameZ.setTime(date);
        functionNameZ.setDeviceGUID(guid);
        functionNameZ.setFunction("{\"function\" :\"read_register_float32\",\"regaddr\":22103,\"regcount\":2}");
        functionsMapper.insterZThreshold(functionNameZ);

        FunctionName functionName = new FunctionName();
        functionName.setTime(date);
        functionName.setDeviceGUID(guid);
        functionName.setFunction("{\"function\" :\"read_register_uint16\",\"regaddr\":22092,\"regcount\":1}");
        functionsMapper.insterAlarmSwitch(functionName);

        CompletableFuture<List<FunctionResultInFo>> future = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000); // 等待5秒钟
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return functionsMapper.findThreshold(date);
        });
        // 等待异步操作完成并获取结果
        List<FunctionResultInFo> threshold = future.join();
        AlarmRecord alarmRecord = alarmRecordMapper.selectStatus(guid);
        if (ObjectUtils.isEmpty(alarmRecord)) {
            threshold.add(4,accelerationMapper.selectTimePeak(guid));
            threshold.add(5,deviceMapper.selectMinimum(guid));
            return threshold;
        } else {
            Date date1 = alarmRecord.getDate();
            threshold.add(4,accelerationMapper.selectTimePeaks(date1, guid));
            threshold.add(5,deviceMapper.selectMinimum(guid));
            return threshold;
        }
    }

    /**
     * 报警开关
     *
     * @param value
     * @param guid
     * @return
     */
    @Override
    public Result alarmSwitch(Integer value, String guid) {

        AlarmRecordInfo alarmRecordInfo = alarmRecordMapper.selectListAlarm();
        if (alarmRecordInfo != null) {
            return Result.fail("当前发生碰撞，无法关闭报警");
        }
        if (value.equals(0)) {
            Date date = new Date();
            date.setTime(date.getTime() / 1000 * 1000);
            //关闭采集
            FunctionName functionName = new FunctionName();
            functionName.setTime(date);
            functionName.setDeviceGUID(guid);
            functionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":0}");
            functionsMapper.insertCloseGather(functionName);
            //开启X阈值报警
            FunctionName functionNameX = new FunctionName();
            functionNameX.setTime(date);
            functionNameX.setDeviceGUID(guid);
            functionNameX.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":22092,\"regcount\":1,\"regcontent\":1}");
            functionsMapper.insterXThreshold(functionNameX);
            //开启Y阈值报警
            FunctionName functionNameY = new FunctionName();
            functionNameY.setTime(date);
            functionNameY.setDeviceGUID(guid);
            functionNameY.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":22097,\"regcount\":1,\"regcontent\":1}");
            functionsMapper.insterYThreshold(functionNameY);
            //开启Z阈值报警
            FunctionName functionNameZ = new FunctionName();
            functionNameZ.setTime(date);
            functionNameZ.setDeviceGUID(guid);
            functionNameZ.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":22102,\"regcount\":1,\"regcontent\":1}");
            functionsMapper.insterZThreshold(functionNameZ);
            //清除报警
            FunctionName functionName2 = new FunctionName();
            functionName2.setTime(date);
            functionName2.setDeviceGUID(guid);
            functionName2.setFunction("{\"function\" :\"write_register_uint32\",\"regaddr\":22266,\"regcount\":2,\"regcontent\":15}");
            functionsMapper.clearAlarm1(functionName2);
            //开启采集
            Date date1 = new Date();
            FunctionName startFunctionName = new FunctionName();
            startFunctionName.setTime(date1);
            startFunctionName.setDeviceGUID(guid);
            startFunctionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":1}");
            functionsMapper.insertStart(startFunctionName);
            return Result.ok("开启成功");
        } else {
            Date date = new Date();
            date.setTime(date.getTime() / 1000 * 1000);
            //关闭采集
            FunctionName functionName = new FunctionName();
            functionName.setTime(date);
            functionName.setDeviceGUID(guid);
            functionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":0}");
            functionsMapper.insertCloseGather(functionName);
            //关闭X阈值报警
            FunctionName functionNameX = new FunctionName();
            functionNameX.setTime(date);
            functionNameX.setDeviceGUID(guid);
            functionNameX.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":22092,\"regcount\":1,\"regcontent\":0}");
            functionsMapper.insterXThreshold(functionNameX);
            //关闭Y阈值报警
            FunctionName functionNameY = new FunctionName();
            functionNameY.setTime(date);
            functionNameY.setDeviceGUID(guid);
            functionNameY.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":22097,\"regcount\":1,\"regcontent\":0}");
            functionsMapper.insterYThreshold(functionNameY);
            //关闭Z阈值报警
            FunctionName functionNameZ = new FunctionName();
            functionNameZ.setTime(date);
            functionNameZ.setDeviceGUID(guid);
            functionNameZ.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":22102,\"regcount\":1,\"regcontent\":0}");
            functionsMapper.insterZThreshold(functionNameZ);
            //开启采集
            Date date1 = new Date();
            FunctionName startFunctionName = new FunctionName();
            startFunctionName.setTime(date1);
            startFunctionName.setDeviceGUID(guid);
            startFunctionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":1}");
            functionsMapper.insertStart(startFunctionName);
        }
        return Result.ok("关闭成功");
    }

    /**
     * 清除报警
     *
     * @param guid
     * @param machineId
     * @return
     */
    @Override
    public Result clearAlarm(String guid, Long machineId) {
        Date date = new Date();
        date.setTime(date.getTime() / 1000 * 1000);
        FunctionName functionName = new FunctionName();
        functionName.setTime(date);
        functionName.setDeviceGUID(guid);
        functionName.setFunction("{\"function\" :\"write_register_uint32\",\"regaddr\":22266,\"regcount\":2,\"regcontent\":15}");
        functionsMapper.clearAlarm(functionName);
        alarmRecordMapper.updateMachineId(machineId);
        alarmeventlogMapper.updateLog(guid);
        return Result.ok("清除报警成功");
    }

    /**
     * 修改阈值
     *
     * @param guid
     * @param machineId
     * @param x
     * @param y
     * @param z
     * @return
     */
    @Override
    public Result updateThreshold(String guid, Long machineId, Double x, Double y, Double z) {
        ThresholdInfo thresholdInfo = thresholdMapper.selectThreshold(machineId);
        if (thresholdInfo == null) {
            Date date1 = new Date();
            date1.setTime(date1.getTime() / 1000 * 1000);
            //关闭采集
            FunctionName functionName = new FunctionName();
            functionName.setTime(date1);
            functionName.setDeviceGUID(guid);
            functionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":0}");
            functionsMapper.insertCloseGather(functionName);
            //修改x轴阈值
            Date date = new Date();
            date.setTime(date.getTime() / 1000 * 1000);
            FunctionName functionNameX = new FunctionName();
            functionNameX.setDeviceGUID(guid);
            functionNameX.setTime(date);
            WriteOperation writeOperationX = new WriteOperation();
            writeOperationX.setFunction("write_register_float32");
            writeOperationX.setRegaddr(22093);
            writeOperationX.setRegcount(2);
            writeOperationX.setRegcontent(x);
            functionNameX.setFunction(String.valueOf(writeOperationX));
            functionsMapper.insertThreshold(functionNameX);
            //修改Y轴阈值
            FunctionName functionNameY = new FunctionName();
            functionNameY.setDeviceGUID(guid);
            functionNameY.setTime(date);
            WriteOperation writeOperationY = new WriteOperation();
            writeOperationY.setFunction("write_register_float32");
            writeOperationY.setRegaddr(22098);
            writeOperationY.setRegcount(2);
            writeOperationY.setRegcontent(y);
            functionNameY.setFunction(String.valueOf(writeOperationY));
            functionsMapper.insertThreshold(functionNameY);
            //修改Z轴阈值
            FunctionName functionNameZ = new FunctionName();
            functionNameZ.setDeviceGUID(guid);
            functionNameZ.setTime(date);
            WriteOperation writeOperationZ = new WriteOperation();
            writeOperationZ.setFunction("write_register_float32");
            writeOperationZ.setRegaddr(22103);
            writeOperationZ.setRegcount(2);
            writeOperationZ.setRegcontent(z);
            functionNameZ.setFunction(String.valueOf(writeOperationZ));
            functionsMapper.insertThreshold(functionNameZ);
            //清除报警
            FunctionName functionName2 = new FunctionName();
            functionName2.setTime(date);
            functionName2.setDeviceGUID(guid);
            functionName2.setFunction("{\"function\" :\"write_register_uint32\",\"regaddr\":22266,\"regcount\":2,\"regcontent\":15}");
            functionsMapper.clearAlarm1(functionName2);
            //新增修改阈值记录
            Threshold threshold = new Threshold();
            threshold.setDate(date);
            threshold.setMachineId(machineId);
            threshold.setXThreshold(x);
            threshold.setYThreshold(y);
            threshold.setZThreshold(z);
            WxUserInfo wxUserInfo = (WxUserInfo) StpUtil.getSession().get("wxUserInfo");
            String name = wxUserInfo.getName();
            threshold.setName(name);
            thresholdMapper.insertThreshold(threshold);
            //开启采集
            Date date2 = new Date();
            FunctionName startFunctionName = new FunctionName();
            startFunctionName.setTime(date2);
            startFunctionName.setDeviceGUID(guid);
            startFunctionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":1}");
            functionsMapper.insertStart(startFunctionName);
        } else {
            Double xThreshold = thresholdInfo.getXThreshold();
            Double yThreshold = thresholdInfo.getYThreshold();
            Double zThreshold = thresholdInfo.getZThreshold();
            if (x > xThreshold && y > yThreshold && z > zThreshold) {
                Date date1 = new Date();
                date1.setTime(date1.getTime() / 1000 * 1000);
                //关闭采集
                FunctionName functionName = new FunctionName();
                functionName.setTime(date1);
                functionName.setDeviceGUID(guid);
                functionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":0}");
                functionsMapper.insertCloseGather(functionName);
                //修改x轴阈值
                Date date = new Date();
                date.setTime(date.getTime() / 1000 * 1000);
                FunctionName functionNameX = new FunctionName();
                functionNameX.setDeviceGUID(guid);
                functionNameX.setTime(date);
                WriteOperation writeOperationX = new WriteOperation();
                writeOperationX.setFunction("write_register_float32");
                writeOperationX.setRegaddr(22093);
                writeOperationX.setRegcount(2);
                writeOperationX.setRegcontent(x);
                functionNameX.setFunction(String.valueOf(writeOperationX));
                functionsMapper.insertThreshold(functionNameX);
                //修改Y轴阈值
                FunctionName functionNameY = new FunctionName();
                functionNameY.setDeviceGUID(guid);
                functionNameY.setTime(date);
                WriteOperation writeOperationY = new WriteOperation();
                writeOperationY.setFunction("write_register_float32");
                writeOperationY.setRegaddr(22098);
                writeOperationY.setRegcount(2);
                writeOperationY.setRegcontent(y);
                functionNameY.setFunction(String.valueOf(writeOperationY));
                functionsMapper.insertThreshold(functionNameY);
                //修改Z轴阈值
                FunctionName functionNameZ = new FunctionName();
                functionNameZ.setDeviceGUID(guid);
                functionNameZ.setTime(date);
                WriteOperation writeOperationZ = new WriteOperation();
                writeOperationZ.setFunction("write_register_float32");
                writeOperationZ.setRegaddr(22103);
                writeOperationZ.setRegcount(2);
                writeOperationZ.setRegcontent(z);
                functionNameZ.setFunction(String.valueOf(writeOperationZ));
                functionsMapper.insertThreshold(functionNameZ);
                //清除报警
                FunctionName functionName2 = new FunctionName();
                functionName2.setTime(date);
                functionName2.setDeviceGUID(guid);
                functionName2.setFunction("{\"function\" :\"write_register_uint32\",\"regaddr\":22266,\"regcount\":2,\"regcontent\":15}");
                functionsMapper.clearAlarm1(functionName2);
                //新增修改阈值记录
                Threshold threshold = new Threshold();
                threshold.setDate(date);
                threshold.setMachineId(machineId);
                threshold.setXThreshold(x);
                threshold.setYThreshold(y);
                threshold.setZThreshold(z);
                WxUserInfo wxUserInfo = (WxUserInfo) StpUtil.getSession().get("wxUserInfo");
                String name = wxUserInfo.getName();
                threshold.setName(name);
                thresholdMapper.insertThreshold(threshold);
                //开启采集
                Date date2 = new Date();
                FunctionName startFunctionName = new FunctionName();
                startFunctionName.setTime(date2);
                startFunctionName.setDeviceGUID(guid);
                startFunctionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":1}");
                functionsMapper.insertStart(startFunctionName);
                return Result.ok("修改成功");
            }
            return Result.fail("修改阈值小于当前阈值，请重新设置。");
        }
        return Result.ok("修改成功");
    }

    /**
     * 机床停机时将阈值改为保底值
     */
    @Scheduled(fixedRate = 1000)
    @Override
    public void updateAllThreshold() {
        //查询所以机床
        List<DeviceInfo> devices = deviceMapper.selectDevice();
        for (DeviceInfo device : devices) {
            //查询机床的震动值
//            List<AccelerationInfo> accelerationInfos = accelerationMapper.selectaccelerations(device);
//            List<Double> peaks = accelerationInfos.stream().map(accelerationInfo1 -> accelerationInfo1.getPeak()).toList();
//            Double minPeak = Collections.min(peaks);
//            if(minPeak > device.getOperation()){
            AccelerationInfo accelerationInfo = accelerationMapper.selectacceleration(device);
            //AccelerationInfo accelerationInfo = accelerationInfos.get(0);
            if(!ObjectUtils.isEmpty(accelerationInfo)){
                Double peak = accelerationInfo.getPeak();
                Date time = accelerationInfo.getTime();
                String deviceGUID = accelerationInfo.getDeviceGUID();
                Double minimum = device.getMinimum();
                Integer status = device.getStatus();
                Double operation = device.getOperation();
                AccelerationInfo accelerationInfoTime = accelerationMapper.selectpeak(deviceGUID, time);
                Double timePeak = accelerationInfoTime.getPeak();
                if (peak < operation && status == 1 && timePeak > operation) {
                    //修改状态
                    deviceMapper.updateStatus(deviceGUID);
                    //修改x轴阈值
                    FunctionName functionNameX = new FunctionName();
                    functionNameX.setDeviceGUID(deviceGUID);
                    functionNameX.setTime(time);
                    WriteOperation writeOperationX = new WriteOperation();
                    writeOperationX.setFunction("write_register_float32");
                    writeOperationX.setRegaddr(22093);
                    writeOperationX.setRegcount(2);
                    writeOperationX.setRegcontent(minimum);
                    functionNameX.setFunction(String.valueOf(writeOperationX));
//                functionsMapper.insertThreshold(functionNameX);
                    //修改Y轴阈值
                    FunctionName functionNameY = new FunctionName();
                    functionNameY.setDeviceGUID(deviceGUID);
                    functionNameY.setTime(time);
                    WriteOperation writeOperationY = new WriteOperation();
                    writeOperationY.setFunction("write_register_float32");
                    writeOperationY.setRegaddr(22098);
                    writeOperationY.setRegcount(2);
                    writeOperationY.setRegcontent(minimum);
                    functionNameY.setFunction(String.valueOf(writeOperationY));
//                functionsMapper.insertThreshold(functionNameY);
                    //修改Z轴阈值
                    FunctionName functionNameZ = new FunctionName();
                    functionNameZ.setDeviceGUID(deviceGUID);
                    functionNameZ.setTime(time);
                    WriteOperation writeOperationZ = new WriteOperation();
                    writeOperationZ.setFunction("write_register_float32");
                    writeOperationZ.setRegaddr(22103);
                    writeOperationZ.setRegcount(2);
                    writeOperationZ.setRegcontent(minimum);
                    functionNameZ.setFunction(String.valueOf(writeOperationZ));
//                functionsMapper.insertThreshold(functionNameZ);
                    //关闭采集
                    FunctionName functionName = new FunctionName();
                    functionName.setTime(time);
                    functionName.setDeviceGUID(deviceGUID);
                    functionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":0}");
//                functionsMapper.insertCloseGather(functionName);
                    try {
                        Thread.sleep(3000);
                        //开启采集
                        FunctionName startFunctionName = new FunctionName();
                        startFunctionName.setTime(time);
                        startFunctionName.setDeviceGUID(deviceGUID);
                        startFunctionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":1}");
//                    functionsMapper.insertStart(startFunctionName);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }


        }
//        }
    }

    /**
     * 机床加工时将阈值改为加工时的最大震动值
     */
    @Scheduled(fixedRate = 1000)
    @Override
    public void updateThresholdTime() {
        List<DeviceInfo> devices = deviceMapper.selectDevice();
        for (DeviceInfo device : devices) {
            List<AccelerationInfo> accelerationInfos = accelerationMapper.selectaccelerations(device);
            if(!accelerationInfos.isEmpty()){
                List<Double> peaks = accelerationInfos.stream().map(accelerationInfo1 -> accelerationInfo1.getPeak()).toList();
                Double minPeak = Collections.min(peaks);
                if (minPeak > device.getOperation()) {
                    AccelerationInfo accelerationInfo = accelerationInfos.get(0);
                    //查询机床的震动值
                    // AccelerationInfo accelerationInfo = accelerationMapper.selectacceleration(device);
                    Double peak = accelerationInfo.getPeak();
                    Date time = accelerationInfo.getTime();
                    String deviceGUID = accelerationInfo.getDeviceGUID();
                    Double standard = device.getStandard();
                    Double percentage = device.getPercentage();
                    Integer status = device.getStatus();
                    if (peak > standard && status == 0) {
                        //修改状态
                        deviceMapper.updateStatu(deviceGUID);
                        try {
                            Thread.sleep(5000);
                            Date endDate = new Date();
                            XThreshold xPeak = accelerationMapper.seleltXPeak(deviceGUID, time, endDate);
                            Double xThreshold = xPeak.getXThreshold();
                            xThreshold = xThreshold + xThreshold + percentage;
                            YThreshold yPeak = accelerationMapper.seleltYPeak(deviceGUID, time, endDate);
                            Double yThreshold = yPeak.getYThreshold();
                            yThreshold = yThreshold + yThreshold + percentage;
                            ZThreshold zPeak = accelerationMapper.seleltZPeak(deviceGUID, time, endDate);
                            Double zThreshold = zPeak.getZThreshold();
                            zThreshold = zThreshold * zThreshold + percentage;
                            Date date1 = new Date();
                            date1.setTime(date1.getTime() / 1000 * 1000);
                            //修改x轴阈值
                            FunctionName functionNameX = new FunctionName();
                            functionNameX.setDeviceGUID(deviceGUID);
                            functionNameX.setTime(endDate);
                            WriteOperation writeOperationX = new WriteOperation();
                            writeOperationX.setFunction("write_register_float32");
                            writeOperationX.setRegaddr(22093);
                            writeOperationX.setRegcount(2);
                            writeOperationX.setRegcontent(xThreshold);
                            functionNameX.setFunction(String.valueOf(writeOperationX));
//                    functionsMapper.insertThreshold(functionNameX);
                            //修改Y轴阈值
                            FunctionName functionNameY = new FunctionName();
                            functionNameY.setDeviceGUID(deviceGUID);
                            functionNameY.setTime(endDate);
                            WriteOperation writeOperationY = new WriteOperation();
                            writeOperationY.setFunction("write_register_float32");
                            writeOperationY.setRegaddr(22098);
                            writeOperationY.setRegcount(2);
                            writeOperationY.setRegcontent(yThreshold);
                            functionNameY.setFunction(String.valueOf(writeOperationY));
//                    functionsMapper.insertThreshold(functionNameY);
                            //修改Z轴阈值
                            FunctionName functionNameZ = new FunctionName();
                            functionNameZ.setDeviceGUID(deviceGUID);
                            functionNameZ.setTime(endDate);
                            WriteOperation writeOperationZ = new WriteOperation();
                            writeOperationZ.setFunction("write_register_float32");
                            writeOperationZ.setRegaddr(22103);
                            writeOperationZ.setRegcount(2);
                            writeOperationZ.setRegcontent(zThreshold);
                            functionNameZ.setFunction(String.valueOf(writeOperationZ));
//                    functionsMapper.insertThreshold(functionNameZ);
                            //关闭采集
                            FunctionName functionName = new FunctionName();
                            functionName.setTime(endDate);
                            functionName.setDeviceGUID(deviceGUID);
                            functionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":0}");
//                    functionsMapper.insertCloseGather(functionName);
                            try {
                                Thread.sleep(3000);
                                //开启采集
                                FunctionName startFunctionName = new FunctionName();
                                startFunctionName.setTime(endDate);
                                startFunctionName.setDeviceGUID(deviceGUID);
                                startFunctionName.setFunction("{\"function\" :\"write_register_uint16\",\"regaddr\":4,\"regcount\":1,\"regcontent\":1}");
//                        functionsMapper.insertStart(startFunctionName);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }

        }
    }

    /**
     * 查找所以设备
     *
     * @return
     */
    @Override
    public List<Device> findDevieve() {
        return deviceMapper.findDevieve();
    }


    /**
     * 修改设备基准值和保底值
     *
     * @param standard
     * @param minimum
     * @param id
     * @return
     */
    @Override
    public Result updateDevice(Double standard, Double minimum, Integer id, Double percentage, Double operation) {
        if (standard != null && minimum != null) {
            deviceMapper.updateDevice(standard, minimum, id, percentage, operation);
            return Result.ok("修改成功");
        }
        return Result.fail("修改失败");
    }
}
